package io.github.zeleven.daysmatter;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by user on 17-8-29.
 */

public class EventDetailActivity extends BaseActivity {

    private EventDao eventDao;
    private long eventId;

    private TextView detailDate;
    private TextView detailContent;
    private TextView detailDays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.layoutId = R.layout.activity_event_detail;
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_event_detail);

        Intent intent = getIntent();
        eventId = intent.getLongExtra("eventId", -1);
        String eventContent = intent.getStringExtra("eventContent");
        long eventDateMs = intent.getLongExtra("eventTargetDate", -1);
        long daysCount = intent.getLongExtra("eventDaysCount", -1);

        detailContent = (TextView) findViewById(R.id.event_detail_content);
        detailDays = (TextView) findViewById(R.id.event_detail_days);
        detailDate = (TextView) findViewById(R.id.event_target_date);
        Calendar c = Calendar.getInstance();
        new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());

        if(eventDateMs != -1) {
            detailDate.setText(new SimpleDateFormat("yyyy-MM-dd").
                    format(new Date(eventDateMs)));
        }
        detailContent.setText(eventContent);
        if(daysCount != -1) {
            detailDays.setText(Long.toString(daysCount));
        }

        eventDao = ((App) getApplication()).getDaoSession().getEventDao();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.detail_toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete_event:
                AlertDialog.Builder builder = new AlertDialog.Builder(EventDetailActivity.this);
                builder.setMessage("确定要删除吗？");
                builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        eventDao.deleteByKey(eventId);
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.show();
                return true;
            case R.id.edit_event:
                Intent intent = new Intent(EventDetailActivity.this, EventEditActivity.class);
                intent.putExtra("eventId", eventId);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Event event = eventDao.queryBuilder().where(EventDao.Properties.Id.eq(eventId)).
                list().get(0);
        Date date = event.getTargetDate();
//        long duration = getDateDuration(c.getTime(), date);
        long duration = Utility.getDateInterval(date);
        detailDate.setText(Utility.dateToString(date));
        detailContent.setText(event.getEventContent());
        detailDays.setText(Long.toString(duration));
    }
}
